package com.text.controller;

import com.github.pagehelper.PageInfo;
import com.text.entity.OrdersItem;
import com.text.service.OrdersItemService;
import jakarta.annotation.Resource;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.TimeUnit;


/**
 * (OrdersItem)表控制层
 *
 * @author makejava
 * @since 2023-06-30 16:30:23
 */
@RestController
@RequestMapping("/admin")
public class OrdersItemController {
    /**
     * 服务对象
     */
    @Resource
    private OrdersItemService ordersItemService;

    @Resource
    private RedisTemplate redisTemplate;

    /**
     * 分页查询
     *
     * @param ordersItem 筛选条件
     * @return 查询结果
     */
    @GetMapping("/OrdersItemList")
    public ResponseEntity<PageInfo<OrdersItem>> queryByPage(@RequestBody(required = false) OrdersItem ordersItem,
                                                        @RequestParam(value = "pageNum",required = false,defaultValue = "1") Integer pageNum,
                                                        @RequestParam(value = "pageSize",required = false,defaultValue = "5") Integer pageSize) {
        PageInfo<OrdersItem> ordersItemListPage = (PageInfo<OrdersItem>) this.ordersItemService.queryByPage(ordersItem,pageNum,pageSize);
        return ResponseEntity.ok(ordersItemListPage);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/hyq/{id}")
    public ResponseEntity<OrdersItem> queryById(@PathVariable("id") Integer id) {
        Object object = redisTemplate.opsForValue().get("orderItem_" + id);
        if (object != null) {
            return ResponseEntity.ok((OrdersItem) object);
        }
        OrdersItem orderItem = this.ordersItemService.queryById(id);
        redisTemplate.opsForValue().set("orderItem_" + id,orderItem,1, TimeUnit.DAYS);
        return ResponseEntity.ok(orderItem);
    }

    /**
     * 新增数据
     *
     * @param ordersItem 实体
     * @return 新增结果
     */
    @PostMapping("/ordersItemAdd")
    public ResponseEntity<OrdersItem> add(OrdersItem ordersItem) {
        return ResponseEntity.ok(this.ordersItemService.insert(ordersItem));
    }

    /**
     * 编辑数据
     *
     * @param ordersItem 实体
     * @return 编辑结果
     */
    @PutMapping("/ordersItemEdit")
    public ResponseEntity<OrdersItem> edit(@RequestBody OrdersItem ordersItem) {
        OrdersItem o = this.ordersItemService.update(ordersItem);
        redisTemplate.opsForValue().set("orderItem_" + ordersItem.getId(),o);
        return ResponseEntity.ok(o);
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping("/ordersItemDelete")
    public ResponseEntity<Boolean> deleteById(Integer id) {
        boolean f =this.ordersItemService.deleteById(id);
        redisTemplate.delete("orderItem_" + id);
        return ResponseEntity.ok(f);
    }

}

